﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using DDD;
using FloatComparer = UnitTest.TestHelper.FloatComparer;

namespace UnitTest {
    [TestClass]
    public class TestDepthTest_Disabled_ {

        #region Field
        GameWindow window;
        World wld;
        Graphics3D g3d;
        FrameBuffer fbuf;
        #endregion


        #region Setup
        [TestInitialize ()]
        public void MyClassInitialize () {
            window = new GameWindow ();
            wld = CreateWorld ();
            fbuf = CreateFrameBuffer ();

            g3d = Graphics3D.GetInstance ();
            g3d.Reset ();

            g3d.RenderPass += delegate {
                g3d.SetFrameBuffer (fbuf);
                g3d.SetWorld (wld);
                g3d.SetViewort (0, 0, 1, 1);
            };
        }

        [TestCleanup ()]
        public void MyClassCleanup () {
            window.Dispose ();
            window = null;
            wld = null;
        }

        private static FrameBuffer CreateFrameBuffer () {
            var fbuf = new FrameBuffer (1, 1, 0);
            var color = new Image2D<float> (DDD.PixelFormat.R32f, 1, 1);
            var depth = new Image2D<float> (DDD.PixelFormat.Depth, 1, 1);
            fbuf.SetColorBuffer (0, color);
            fbuf.SetDepthBuffer (depth);
            return fbuf;
        }


        private World CreateWorld () {
            var cam = new Camera ();
            cam.SetPerspective (45, 1, 0.1f, 10f);

            var camNode = new Node ();
            camNode.Attach (cam);
            camNode.SetTranslation (0, 0, 1);

            // NDC座標系はZ=[-1,1]で1が奥。
            var posArray = new VertexArray<float> (4, 3);
            posArray.Set (0, 4, new float[12] { 0, 0, -0.5f,
                                                0, 0, 0.1f,
                                                0, 0, 0.1f,
                                                0, 0, 0.5f});
            var colArray = new VertexArray<float> (4, 1);
            colArray.Set (0, 4, new float[4] { 1, 2, 3, 4 });

            var vbuf = new VertexBuffer ();
            vbuf.AddVertexArray ("v_Position", posArray);
            vbuf.AddVertexArray ("v_Color", colArray);

            var ibuf = new IndexBuffer<int> (PrimitiveType.Point, 4);
            ibuf.Set (0, 4, new int[] { 0, 1, 2, 3 });

            var vert = @"#version 400
                         in  vec3  v_Position;
                         in  float v_Color;
                         out float f_Color;

                         void main () {
                             gl_Position = vec4(v_Position, 1);
                             f_Color = v_Color;
                         }
                        ";
            var frag = @"#version 400
                         in  float f_Color;
                         out float frag_Color;
                         
                         void main () {
                              frag_Color = f_Color;  
                         }
                        ";

            var vs = new VertexShader (new MemoryStream (Encoding.UTF8.GetBytes (vert)));
            var fs = new FragmentShader (new MemoryStream (Encoding.UTF8.GetBytes (frag)));
            var sp = new ShaderProgram (vs, fs);

            var cmode = new CompositeMode ();
            cmode.SetDepthTestEnable (false);

            var app = new Appearance ();
            app.SetShaderProgram (sp);
            app.SetComositeMode (cmode);

            var msh = new Mesh ();
            msh.SetVertexBuffer (vbuf);
            msh.SetIndexBuffer (ibuf);
            msh.SetAppearance (app);

            var mshNode = new Node ();
            mshNode.Attach (msh);

            var wld = new World ();
            wld.AddChild (camNode);
            wld.AddChild (mshNode);
            wld.SetActiveCamera (camNode);

            return wld;
        }

        #endregion



        #region TestMethod
        [TestMethod]
        public void TestMethod_TestDepthTest_Disabled_ () {

            g3d.Render ();
            window.SwapBuffers ();

            var expected0 = new float[1] { 4 };
            var expected1 = new float[1] { 1.0f };   // デプステスト無効で書き込みも無効化される
            var pixels0 = new float[1];
            var pixels1 = new float[1];
            var color = fbuf.GetColorBuffer (0) as Image2D<float>;
            var depth = fbuf.GetDepthBuffer () as Image2D<float>;
            color.Capture ();
            depth.Capture ();
            color.Get (0, 0, 1, 1, pixels0);
            depth.Get (0, 0, 1, 1, pixels1);

            CollectionAssert.AreEqual (expected0, pixels0, new FloatComparer (0.01f));
            CollectionAssert.AreEqual (expected1, pixels1, new FloatComparer (0.01f));
        }
        #endregion



    }
}
